//
//  MoreActivityViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/25.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "MoreActivityViewController.h"
#import "MoreActivityModel.h"
#import "CustomerNetworking.h"
#import "MoreActivityTableViewCell.h"
#import "MoreMusicDetailViewController.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)
@interface MoreActivityViewController ()<UITableViewDataSource, UITableViewDelegate, MBProgressHUDDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, retain) MBProgressHUD *HUD;

@property (nonatomic, retain) UIView *zhanView;

@end

@implementation MoreActivityViewController
- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.page = 1;
    self.dataArray = [NSMutableArray array];
    [self createTableView];
    [self loadData];

}
-(void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT_PRO(64)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.zhanView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.zhanView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.zhanView];
    [_zhanView release];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self.tableView.mj_footer beginRefreshing];
        self.page ++;
        [self loadData];
    }];
    
}

-(void)loadData
{
    self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD.delegate = self;
    
    self.HUD.labelText = @"正在进行加载~";
    
    [CustomerNetworking customerWithURL:[NSString stringWithFormat:@"http://api.dongting.com/misc/activezone/new?page=%ld&size=10&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89506531298649&longtitude=121.5311736886395", _page] andParamer:nil andBlock:^(id response) {
        for (NSDictionary *dic in [response objectForKey:@"data"]) {
            MoreActivityModel *moreActivity = [MoreActivityModel modelWithDictionary:dic];
            [self.dataArray addObject:moreActivity];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self.HUD hide:YES];
        [self.zhanView removeFromSuperview];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT_PRO(150);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreActivityModel *moreActivity = _dataArray[indexPath.row];
    MoreActivityTableViewCell *cell = [MoreActivityTableViewCell moreActivityTableViewCellWith:tableView];
    cell.moreActivityModel = moreActivity;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MoreActivityModel *moreActivity = _dataArray[indexPath.row];
    MoreMusicDetailViewController *moreMusicDetail = [[MoreMusicDetailViewController alloc] init];
    moreMusicDetail.strUrl = moreActivity.actionUrl;
    [self.navigationController pushViewController:moreMusicDetail animated:YES];
    [moreMusicDetail release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
